﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.ServiceReference1;
using System.Data.Services.Client;
using System.IO;
using System.Data;
using System.Collections;

namespace WebApplication1
{
    public partial class ProgramCampionat : System.Web.UI.Page
    {
        private Uri serviceUri = new Uri("http://localhost:50000/FootballService.svc/");
        WebApplication1.ServiceReference1.FotbalEntities context;
        protected void Page_Load(object sender, EventArgs e)
        {
            context = new WebApplication1.ServiceReference1.FotbalEntities(serviceUri);
            if (!IsPostBack)
            {
                BindEtape();              

            }

        }
        protected void AniCampionat_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindEtape();
        }
        public void BindEtape()
        {
            DataServiceQuery<WebApplication1.ServiceReference1.ViewProgram> etape =
            context.CreateQuery<WebApplication1.ServiceReference1.ViewProgram>("/ViewPrograms");


           string queryString = string.Format("GetEtapeCampionat?an='{0}'", AniCampionat.Text);
           var results = context.Execute<ViewProgram>(new Uri(queryString, UriKind.Relative));
          
            ListView_ListaMeciuri.DataSource = results;
            ListView_ListaMeciuri.DataBind();
        }
        public void ListView_ListaMeciuriPerEtapa_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            DataServiceQuery<WebApplication1.ServiceReference1.ViewProgram> etape =
            context.CreateQuery<WebApplication1.ServiceReference1.ViewProgram>("/ViewPrograms");

            Label lblID = (Label)this.FindControlRecursive(this,"Label1");//finding control in Listview1 control
            ListView list2 = (ListView)this.FindControlRecursive(this, "ListView_ListaMeciuriPerEtapa");

            var results = from e2 in etape
                          where e2.etapa == Convert.ToInt16(lblID.Text) && e2.AnCampionat == AniCampionat.Text
                        select e2;

            list2.DataSource = results;
            list2.DataBind();
         }
        public Control FindControlRecursive(Control rootControl, string controlID)
        {
            if (rootControl.ID == controlID) return rootControl;
            foreach (Control controlToSearch in rootControl.Controls)
            {


                Control controlToReturn =


                            FindControlRecursive(controlToSearch, controlID);


                if (controlToReturn != null) return controlToReturn;


            }


            return null;
        }
       
    }
    
}